package zyc.web;

import com.sun.org.apache.bcel.internal.generic.MethodGen;
import com.sun.org.apache.bcel.internal.generic.NEW;
import zyc.pojo.Page;
import zyc.pojo.Student;
import zyc.pojo.Teacher;
import zyc.service.StudentService;
import zyc.service.imple.StudentServiceImple;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: Code-zyc
 * @date: Created in 2020/12/21 14:10
 * @version: v1.0
 * @modified By:
 */
@WebServlet("/studentServlet")
public class StudentServlet extends BaseServlet {

    StudentService studentService = new StudentServiceImple();

    /**
     * @return :
     * @throws :
     * @descript :获取所有的学生数据
     * @author :ZhaoYicong
     * @date :2020/12/21 14:31
     * @Param: null
     * @since :
     */

    public void getstudents(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Page page = new Page();

        System.out.println(req.getParameter("pn"));

        //获取用户点击的第几页
        if (req.getParameter("pn") != null) {
            Integer pn = Integer.parseInt(req.getParameter("pn"));
            page.setPageNum(pn);
        }
        //利用page 控制查询
        List<Student> students = studentService.getstudents(page);
        page.setToltal(studentService.getCount());
        req.setAttribute("page", page);
        req.setAttribute("students", students);
        req.getRequestDispatcher("pages/show/students.jsp").forward(req, resp);
    }

    /**
     * @return :
     * @throws :
     * @descript :删除对应的学生Byid
     * @author :ZhaoYicong
     * @date :2020/12/21 14:32
     * @Param: null
     * @since :
     */

    public void deletestudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.parseInt(req.getParameter("id"));

        studentService.deletestudent(id);


        resp.sendRedirect("studentServlet?action=getstudents");

    }

    /**
     * @return :
     * @throws :
     * @descript : 修改用户信息
     * @author :ZhaoYicong
     * @date :2020/12/21 15:39
     * @Param: null
     * @since :
     */

    public void updatebrfore(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer id = Integer.parseInt(req.getParameter("id"));
        req.getSession().setAttribute("upid", id);
        req.getRequestDispatcher("studentServlet?action=getstudents").forward(req, resp);


    }

    /**
     * @return :
     * @throws :
     * @descript :修改用户信息
     * @author :ZhaoYicong
     * @date :2020/12/21 16:56
     * @Param: null
     * @since :
     */

    public void updateafter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer id = Integer.parseInt(req.getParameter("id"));
        Integer sid = Integer.parseInt(req.getParameter("sid"));

        String name = req.getParameter("name");
        Double math = Double.parseDouble(req.getParameter("math"));
        int pn = Integer.parseInt(req.getParameter("pn"));
        Student student = new Student(id, sid, name, math);

        Student student1 = studentService.getstudentBysid(sid);

        if (student1 == null || student1.getId().equals(id)) {
            studentService.updatestudent(student);
        } else {
            req.setAttribute("upalert", "学号已经存在!");
            req.getRequestDispatcher("studentServlet?action=updatebrfore").forward(req, resp);

        }


        //清楚标记
        req.getSession().removeAttribute("upid");

        req.getRequestDispatcher("studentServlet?action=getstudents").forward(req, resp);

    }


    public void creatstudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer sid = Integer.parseInt(req.getParameter("sid"));
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        Integer tid = Integer.parseInt(req.getParameter("tid"));
        Double math = Double.parseDouble(req.getParameter("math"));
        Student student = new Student(null, sid, name, math);
        Teacher teacher=new Teacher();
        teacher.setId(tid);
        student.setTeacher(teacher);
        student.setPassword(password);
        Student student1 = studentService.getstudentBysid(student.getSid());
        if (student1 != null) {
            req.setAttribute("name", name);
            req.setAttribute("math", math);
            req.setAttribute("creatalert", "这个学号:" + sid + "已经有了 你是不是输错了！");
        } else {
            studentService.creatStudent(student);

        }

        req.getRequestDispatcher("studentServlet?action=getstudents").forward(req, resp);


    }

    /**
     * @return :
     * @throws :
     * @descript :通过学号和姓名来进行查询
     * @author :ZhaoYicong
     * @date :2020/12/23 22:06
     * @Param: null
     * @since :
     */

    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String type = req.getParameter("type");
        String val = req.getParameter("val");
        req.getSession().removeAttribute("searchtype");
        req.getSession().removeAttribute("searchval");
        List<Student> students = new ArrayList<>();
        if ("sid".equals(type)) {
            int sid = Integer.parseInt(val);
            Student student = studentService.getstudentBysid(sid);
            if(student!=null){
                students.add(student);
            }

        } else if ("name".equals(type)) {
            String name = "%" + val + "%";
            students = studentService.getstudentsByname(name);
        } else {

            Double math = Double.parseDouble(val);
            students = studentService.getstudentsBymath(math);


        }
        if(students.size()==0){
            System.out.println("数学成绩进来");
            req.setAttribute("students", null);
        }else{
            req.setAttribute("students", students);
        }

        req.getSession().setAttribute("searchtype",type);
        req.getSession().setAttribute("searchval",val);


        req.getRequestDispatcher("pages/show/search.jsp").forward(req, resp);

    }

    /**
     * @return :
     * @throws :
     * @descript :排序展开
     * @author :ZhaoYicong
     * @date :2020/12/23 23:30
     * @Param: null
     * @since :
     */

    public void sort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        String way = req.getParameter("way");

        req.getSession().removeAttribute("sorttype");
        req.getSession().removeAttribute("sortway");

        List<Student> students ;
        if ("asc".equals(way)) {
            students = studentService.getstudentsByAsc(type);

        } else {

            students = studentService.getstudentsByDesc(type);
        }

        req.getSession().setAttribute("sorttype",type);
        req.getSession().setAttribute("sortway",way);


        req.setAttribute("students", students);


        req.getRequestDispatcher("pages/show/scoresort.jsp").forward(req, resp);

    }



    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer sid = Integer.parseInt(req.getParameter("id"));
        String password = req.getParameter("password");
        System.out.println(sid);

       Student student= studentService.getstudentBySidPass(sid,password);

       if(student!=null){
           req.getSession().setAttribute("student",student);
           req.getRequestDispatcher("studentServlet?action=getstudentTostudent").forward(req,resp);
       }else{
           req.setAttribute("alert","学号或者密码错误！");
           req.getRequestDispatcher("index.jsp").forward(req,resp);
       }


    }



    //暂时为了答辩设计的函数

    //查询数据到学生观看页面

    public void getstudentTostudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer sid =Integer.parseInt(req.getParameter("id"));
        Student student = studentService.getstudentBysid(sid);
        req.setAttribute("student",student);
        req.getRequestDispatcher("pages/studentshow/students.jsp").forward(req,resp);
    }

    /**
     * @descript :查询
     * @author :ZhaoYicong
     * @date :2020/12/24 9:32
     * @Param: null
     * @return :
     * @throws :
     * @since :
     */


    public void updatebeforsearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer id = Integer.parseInt(req.getParameter("id"));
        req.getSession().setAttribute("upid", id);
        Object searchtype = req.getSession().getAttribute("searchtype");
        Object searchval = req.getSession().getAttribute("searchval");
        req.getRequestDispatcher("studentServlet?action=search&type="+searchtype+"&val="+searchval).forward(req, resp);


    }



    public void updateaftersearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer id = Integer.parseInt(req.getParameter("id"));
        Integer sid = Integer.parseInt(req.getParameter("sid"));
        String name = req.getParameter("name");
        Double math = Double.parseDouble(req.getParameter("math"));
        Student student = new Student(id, sid, name, math);
        Student student1 = studentService.getstudentBysid(sid);
        System.out.println("静茹该方法");
        System.out.println(student1.toString());
        if (student1 == null || student1.getId().equals(id)) {
            studentService.updatestudent(student);
        } else {
            req.setAttribute("upalert", "学号已经存在!");
            req.getRequestDispatcher("studentServlet?action=updatebeforsearch").forward(req, resp);
        }
        Object searchtype = req.getSession().getAttribute("searchtype");
        Object searchval = req.getSession().getAttribute("searchval");
        //清楚标记
        req.getSession().removeAttribute("upid");

        req.getRequestDispatcher("studentServlet?action=search&type="+searchtype+"&val="+searchval).forward(req, resp);
    }

    public void deletestudentsearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.parseInt(req.getParameter("id"));

        studentService.deletestudent(id);


        Object searchtype = req.getSession().getAttribute("searchtype");
        Object searchval = req.getSession().getAttribute("searchval");

        req.getRequestDispatcher("studentServlet?action=search&type="+searchtype+"&val="+searchval).forward(req, resp);



    }

    /**
     * @descript :排序
     * @author :ZhaoYicong
     * @date :2020/12/24 9:35
     * @Param: null
     * @return :
     * @throws :
     * @since :
     */

    public void updatebeforsort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer id = Integer.parseInt(req.getParameter("id"));
        req.getSession().setAttribute("upid", id);
        Object sorttype = req.getSession().getAttribute("sorttype");
        Object sortway = req.getSession().getAttribute("sortway");

        req.getRequestDispatcher("studentServlet?action=sort&type="+sorttype+"&way="+sortway).forward(req, resp);


    }

    public void updateaftersort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Integer id = Integer.parseInt(req.getParameter("id"));
        Integer sid = Integer.parseInt(req.getParameter("sid"));

        String name = req.getParameter("name");
        Double math = Double.parseDouble(req.getParameter("math"));
        Student student = new Student(id, sid, name, math);

        Student student1 = studentService.getstudentBysid(sid);

        if (student1 == null || student1.getId().equals(id)) {
            studentService.updatestudent(student);
        } else {
            req.setAttribute("upalert", "学号已经存在!");
            req.getRequestDispatcher("studentServlet?action=updatebeforsort").forward(req, resp);

        }

        //清楚标记
        req.getSession().removeAttribute("upid");
        Object sorttype = req.getSession().getAttribute("sorttype");
        Object sortway = req.getSession().getAttribute("sortway");

        req.getRequestDispatcher("studentServlet?action=sort&type="+sorttype+"&way="+sortway).forward(req, resp);
    }

    public void deletestudentsort(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Integer id = Integer.parseInt(req.getParameter("id"));

        studentService.deletestudent(id);


        Object sorttype = req.getSession().getAttribute("sorttype");
        Object sortway = req.getSession().getAttribute("sortway");

        req.getRequestDispatcher("studentServlet?action=sort&type="+sorttype+"&way="+sortway).forward(req, resp);
    }


}
